package com.ruoyi.system.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Shelf;
import com.ruoyi.system.service.IShelfService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货架管理Controller
 * 
 * @author yangxp
 * @date 2020-07-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/medicine/shelf" )
public class ShelfController extends BaseController {

    private final IShelfService iShelfService;

    /**
     * 查询货架管理列表
     */
    @PreAuthorize("@ss.hasPermi('medicine:shelf:list')")
    @GetMapping("/list")
    public TableDataInfo list(Shelf shelf)
    {
        startPage();
        LambdaQueryWrapper<Shelf> lqw = new LambdaQueryWrapper<Shelf>();
        if (StringUtils.isNotBlank(shelf.getSheCode())){
            lqw.eq(Shelf::getSheCode ,shelf.getSheCode());
        }
        List<Shelf> list = iShelfService.list(lqw);
        return getDataTable(list);
    }

    /**
     *
     * 导出货架管理列表
     */
    @PreAuthorize("@ss.hasPermi('medicine:shelf:export')" )
    @Log(title = "货架管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Shelf shelf) {
        LambdaQueryWrapper<Shelf> lqw = new LambdaQueryWrapper<Shelf>(shelf);
        List<Shelf> list = iShelfService.list(lqw);
        ExcelUtil<Shelf> util = new ExcelUtil<Shelf>(Shelf. class);
        return util.exportExcel(list, "shelf" );
    }

    /**
     * 获取货架管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('medicine:shelf:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {

        return AjaxResult.success(iShelfService.getById(id));
    }

    /**
     * 新增货架管理
     */
    @PreAuthorize("@ss.hasPermi('medicine:shelf:add')" )
    @Log(title = "货架管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Shelf shelf) {
        shelf.setSheId(IdUtil.fastSimpleUUID());
        shelf.setGmtCreate(DateUtil.date());
        shelf.setGmtModified(DateUtil.date());
        return toAjax(iShelfService.save(shelf) ? 1 : 0);
    }

    /**
     * 修改货架管理
     */
    @PreAuthorize("@ss.hasPermi('medicine:shelf:edit')" )
    @Log(title = "货架管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shelf shelf) {
        return toAjax(iShelfService.updateById(shelf) ? 1 : 0);
    }

    /**
     * 删除货架管理
     */
    @PreAuthorize("@ss.hasPermi('medicine:shelf:remove')" )
    @Log(title = "货架管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iShelfService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
